Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-query-complexity

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-query-complexity

Validation rule for GraphQL query complexity analysis

  • 0.7.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
237K
decreased by-21.03%
Maintainers
1
Weekly downloads
 
Created

What is graphql-query-complexity?

The graphql-query-complexity package is a tool for analyzing and limiting the complexity of GraphQL queries. It helps in preventing abuse by ensuring that queries do not exceed a specified complexity threshold, which can be crucial for maintaining the performance and security of a GraphQL API.

What are graphql-query-complexity's main functionalities?

Complexity Analysis

This feature allows you to calculate the complexity of a given GraphQL query. The code sample demonstrates how to use the `getComplexity` function along with a simple estimator to determine the complexity of a basic query.

const { getComplexity, simpleEstimator } = require('graphql-query-complexity');
const { graphql, buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const query = '{ hello }';

const complexity = getComplexity({
  schema,
  query,
  estimators: [
    simpleEstimator({ defaultComplexity: 1 })
  ]
});

console.log('Query Complexity:', complexity);

Complexity Limiting

This feature allows you to enforce a maximum complexity limit on GraphQL queries. The code sample shows how to calculate the complexity of a query and throw an error if it exceeds a predefined maximum complexity.

const { getComplexity, simpleEstimator } = require('graphql-query-complexity');
const { graphql, buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const query = '{ hello }';

const complexity = getComplexity({
  schema,
  query,
  estimators: [
    simpleEstimator({ defaultComplexity: 1 })
  ]
});

const maxComplexity = 10;
if (complexity > maxComplexity) {
  throw new Error(`Query is too complex: ${complexity}. Maximum allowed complexity: ${maxComplexity}`);
}

console.log('Query is within the allowed complexity range.');

Other packages similar to graphql-query-complexity

Keywords

FAQs

Package last updated on 20 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc